package com.kunchi.isb.base.result;

import com.kunchi.isb.base.dto.ISBDto;
import com.kunchi.isb.base.exception.ISBException;
import com.kunchi.isb.base.validate.ISBFieldError;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * ISB 服务返回对象的规范DTO，所有ISB的服务均返回此对象.
 *
 * @param <D> 指定返回的Body的对象类型（将以json的格式体现）
 */
public class ISBResult<D> implements Serializable {


    public enum status_enum {
        SUCCESS,
        REFUSED,
        FAILED
    }

    /**
     * 返回值的状态,默认为success成功.
     */
    @ApiModelProperty(value = "状态，成功返回为success", name = "返回值状态", example = "SUCCESS 代表成功 | REFUSED 代表拒绝 | FAILED 代表异常")
    private status_enum status = status_enum.SUCCESS;


    /**
     * 标记服务拒绝处理(由于验证失败等原因.)
     */
    public void markSuccess() {
        this.status = status_enum.SUCCESS;
    }

    /**
     * 标记服务拒绝处理(由于验证失败等原因.)
     */
    public void markRefused() {
        this.status = status_enum.REFUSED;
    }


    /**
     * 标记服务的处理结果发生了异常.
     */
    public void markFailed() {
        this.status = status_enum.FAILED;
    }

    /**
     * 服务器处理的返回值.
     */
    @ApiModelProperty(value = "服务处理的返回值", name = "服务处理的返回值", example = "{ \"name\":\"张三\", \"age\":24}")
    private D body;

    /**
     * 服务器处理的验证信息(包含所有验证失败的出错字段).
     */
    @ApiModelProperty(value = "服务处理的返回值", name = "服务处理的返回值", example = "{ \"name\":\"张三\", \"age\":24}")
    private List<ISBFieldError> validationErrors = new ArrayList<ISBFieldError>();

    /**
     * 服务器处理过程中抛出的异常(正常情况下是null)
     */
    @ApiModelProperty(value = "异常信息", name = "异常信息", example = "{ \"code\":0 } ")
    private ISBException exception = null;

    /**
     * 服务器接受请求时的原始参数.
     */
    @ApiModelProperty(value = "原始参数", name = "原始参数", example = "{ \"name\":\"张三\", \"age\":24} ")
    private ISBDto originParam = null;

    public status_enum getStatus() {
        return status;
    }

    public void setStatus(status_enum status) {
        this.status = status;
    }

    public D getBody() {
        return body;
    }

    public ISBException getException() {
        return exception;
    }

    public void setException(ISBException exception) {
        this.exception = exception;
    }

    public void setBody(D body) {
        this.body = body;
    }

    public ISBDto getOriginParam() {
        return originParam;
    }

    public void setOriginParam(ISBDto originParam) {
        this.originParam = originParam;
    }

    public List<ISBFieldError> getValidationErrors() {
        return validationErrors;
    }

    public void setValidationErrors(List<ISBFieldError> validationErrors) {
        this.validationErrors = validationErrors;
    }
}
